package com.ssy.lingxi.order.handler.validator;

import com.ssy.lingxi.order.handler.annotation.DeliveryTypeAnnotation;
import com.ssy.lingxi.order.handler.annotation.FreightTypeAnnotation;
import com.ssy.lingxi.order.model.constant.OrderFreightTypeEnum;
import com.ssy.lingxi.order.model.constant.OrderProductDeliverTypeEnum;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;

/**
 * 订单商品运费方式校验注解验证类
 * @author 万宁
 * @version 2.0.0
 * @date 2021-07-15
 */
public class FreightTypeValidator implements ConstraintValidator<FreightTypeAnnotation, Integer> {
    private boolean nullable = true;

    @Override
    public void initialize(FreightTypeAnnotation constraintAnnotation) {
        nullable = constraintAnnotation.nullable();
    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        if(value == null) {
            return nullable;
        }

        return Arrays.stream(OrderFreightTypeEnum.values()).filter(v -> !v.equals(OrderFreightTypeEnum.NONE)).anyMatch(e -> e.getCode().equals(value));
    }
}
